package com.geega.geetest.controller;

import com.geega.geetest.common.JSONResult;
import com.geega.geetest.core.QueryCondition;
import com.geega.geetest.dto.UserIdDto;
import com.geega.geetest.dto.UserSaveDto;
import com.geega.geetest.mapper.UserMapper;
import com.geega.geetest.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/save")
    @ApiOperation(value = "保存用户",notes = "保存用户信息")
    public JSONResult save(@RequestBody @Valid UserSaveDto dto) {
        return JSONResult.success(userService.save(UserMapper.INSTANCE.toUser(dto)),"保存用户信息成功");
    }

    @PostMapping("/deleteById")
    public JSONResult deleteById(@RequestBody @Valid UserIdDto dto) {
        userService.deleteById(dto.getId());
        return JSONResult.success("删除成功");
    }

    @PostMapping("/deleteAll")
    public JSONResult deleteAll() {
        userService.deleteAll();
        return JSONResult.success("删除成功");
    }

    @PostMapping("/findById")
    public JSONResult findById(@RequestBody @Valid UserIdDto dto) {
        return JSONResult.success(userService.findById(dto.getId()),"查询用户成功");
    }

    @PostMapping("/findAll")
    public JSONResult findAll() {
        return JSONResult.success(userService.findAll(),"查询所有成功") ;
    }

    @PostMapping("/findPage")
    public JSONResult findPage(@RequestBody @Valid QueryCondition condition) {
        return JSONResult.success(userService.findPage(condition),"分页查询成功") ;
    }

}
